﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;
using ManagementEffect;

namespace ServerYuGi
{
    public partial class SSupervisor
    {
        void Process_CARD_LENBAI(params object[] param)
        {
            int ID = TurnManager.IDPlayerTrongLuot;

            SPlayer player = LayPlayer(ID);

            SCard card = null;
            foreach (SCard item in player.SoBaiTrenTay)
            {
                if (item.MyYBaseCard.ID == (int)param[0])
                {
                    card = item;
                    break;
                }
            }

            YMSG TrangThai = (YMSG)param[1];
            card.TrangThai = TrangThai;

            if (card.MyYBaseCard.CardType == YType.Normal)
            {                
                if (Players[ID].SoSummonTrenBan == Game.SoBaiToiDaTrenBan)
                {

                    ServerSocket.SendMessageToID(ID,YMSG.CARD_LENBAI_KHONGHOPLE,1);
                    ServerSocket.SendMessageToID(ID, YMSG.CARD_LENBAI);
                }
                else
                {                   
                    player.SoBaiTrenBan.Add(card);
                    player.SoBaiTrenTay.Remove(card);
                    DangKiSript(card);
                    ServerSocket.SendMessageToID(ID, YMSG.CARD_LENBAI_HOPLE,card.MyYBaseCard.ID,card.TrangThai);

                }
            }
          
        }
        void Process_CARD_LENBAI_XONG(params object[] param)
        {            
            SPlayer player = LayPlayerKhacVoiPlayerDangTrongLuot();
            ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI_XONG, param);
        }
        void Process_CARD_LENBAI_SUMMONDACBIET(params object[] param)
        {
            SPlayer player = LayPlayer(TurnManager.IDPlayerTrongLuot);
            SCard SCardSummon = player.LayCardTrenTay((int)param[0]);
            int SoLuongCardThe = (int)param[1];
            YNormalCard cardSummon = SCardSummon.MyYBaseCard as YNormalCard;
            int ByteSendOther = 5 + SoLuongCardThe;

            switch (SoLuongCardThe)
            {
                case 1:
                    if (cardSummon.Star < 5 || cardSummon.Star > 6)
                    {
                        Console.WriteLine("La Bai Nay Khong Phai Summon The 1 la");
                        ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI_KHONGHOPLE);
                        return;
                    }                        
                    break;
                case 2:
                    if (cardSummon.Star < 7)
                    {
                        Console.WriteLine("La Bai Nay Khong Phai Summon The 2 la");
                        ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI_KHONGHOPLE);
                        return;
                    }    
                    break;

            }
            List<SCard> ListCardThe = new List<SCard>();
            for (int i = 0; i < SoLuongCardThe; i++)
            {
                SCard tmp = player.LayCardTrenBan((int)param[2 + i]);
                if (tmp == null)
                {
                    ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI_KHONGHOPLE, 1);
                    ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI);
                    Console.WriteLine("La bai khong ton tai tren ban");
                    return;
                }
                ListCardThe.Add(tmp);
            }

           

            Console.WriteLine("Da qua tat ca cac dieu kien kiem tra");
            ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI_SUMMONDACBIET_HOPLE, param);
            object[] paramOther = new object[ByteSendOther + 1];
            paramOther[0] = SCardSummon.MyYBaseCard.Card_TypeID;
            for (int i = 0; i < ByteSendOther ; i++)
            {
                paramOther[i + 1] = param[i];
            }
            foreach (SCard item in ListCardThe)
            {
                for (int i = 0; i < player.SoBaiTrenBan.Count; i++)
                {
                    if (item == player.SoBaiTrenBan[i])
                    {
                        player.SoBaiTrenBan.Remove(item);
                        break;
                    }
                    
                }
            }
            player.SoBaiTrenBan.Add(SCardSummon);
            player.SoBaiTrenTay.Remove(SCardSummon);
            DangKiSript(SCardSummon);
            SPlayer PlayerOther = LayPlayerKhacVoiPlayerDangTrongLuot();
            ServerSocket.SendMessageToID(PlayerOther.ID, YMSG.CARD_LENBAI_SUMMONDACBIET_XONG, paramOther);
            Console.WriteLine("Len Bai Summon Dat Biet Xong");
        }

        void Process_CARD_LENBAI_MAGIC(params object[] param)
        {
            SPlayer player = LayPlayer(TurnManager.IDPlayerTrongLuot);
            SCard card = player.LayCardTrenTay((int)param[1]);
            if (card != null)
            {
                if (player.SoTrapHoacSpellTrenBan == Game.SoBaiToiDaTrenBan)
                {
                    ServerSocket.SendMessageToID(player.ID, YMSG.CARD_LENBAI_MAGIC_KHONGHOPLE);
                    Console.WriteLine("Len bai Magic khong hop le");
                    return;
                }
                else
                {
                    player.SoBaiTrenBan.Add(card);
                    player.SoBaiTrenTay.Remove(card);
                    ServerSocket.SendMessageToEveryOne(YMSG.CARD_LENBAI_MAGIC_XONG, param);
                    Console.WriteLine("Len bai Magic thanh cong");
                }
                
            }
        }

     
    }


}
